var app = require("nn_app");
cc.Class({
    extends: require(app.subGameName + "_BaseForm"),

    properties: {
        cardPrefab: cc.Prefab,
    },

    OnCreateInit: function () {
        //定义层级
        this.ZorderLv = 7;
        //最大选牌数量
        const ChooseCardLen = {
            "sg": 3,
            "nn": 5,
            "sss": 13
        };
        this.maxLength = ChooseCardLen[app.subGameName];
        //发送按钮
        this.btn_send = this.node.getChildByName("cardBox").getChildByName("btn_send");
        this.AddButton(this.btn_send)
        //关闭按钮
        this.btn_close = this.node.getChildByName("cardBox").getChildByName("btn_close");
        this.AddButton(this.btn_close)
        //牌库节点
        this.content = this.node.getChildByName("scrollView").getChildByName("content");
        //定义牌库数组
        this.CardPool = [
            0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,   //方块 2-A
            0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,   //梅花 2-A
            0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,   //红桃 2-A
            0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,   //黑桃 2-A
            0x41, 0x42
        ];
        //白名单拿牌数组
        this.sendCardList = []
    },

    //添加按钮缩放点击效果
    AddButton: function (buttonNode) {
        // 添加 cc.Button 组件
        let button = buttonNode.addComponent(cc.Button);
        // 设置按钮的点击缩放效果
        button.transition = cc.Button.Transition.SCALE;
        button.zoomScale = 0.95;
        button.duration = 0.1;
        // 添加事件
        buttonNode.on(cc.Node.EventType.TOUCH_END, this.OnClick, this);
    },

    OnShow: function () {
        //展示牌库
        this.ShowGMCardPool();
    },

    //牌池刷新
    ShowGMCardPool: function () {
        //初始化
        this.sendCardList = []
        this.btn_send.active = false;
        this.content.removeAllChildren();
        //增加所有的牌
        for (let i = 0; i < this.CardPool.length; i++) {
            let cardID = this.CardPool[i];
            let cardNode = cc.instantiate(this.cardPrefab);
            cardNode.setScale(0.7)
            this.ShowCard(cardNode, cardID)
            cardNode.name = "" + cardID
            cardNode.cardID = cardID
            this.content.addChild(cardNode);
            this.AddButton(cardNode)
        }
    },

    //储存卡牌信息
    ShowCard(cardNode, cardID) {
        cardNode.pokerSpriteFrame = "";
        cardNode.cardID = cardID;
        cardNode.PokerCard = app[app.subGameName + "_PokerCard"]();
        //展示卡牌精灵
        cardNode.PokerCard.GetPokeCard(cardID, cardNode)
    },

    OnClick(event) {
        let btnName = event.target.name
        switch (btnName) {
            case "btn_close":
                this.CloseForm()
                break;
            case "btn_send":
                this.SendGMCardList()
                this.CloseForm()
                break;
            default:
                this.OnCardClick(event)
                break;
        }
    },

    OnCardClick: function (event) {
        let node = event.target
        let btnCardID = node.cardID
        btnCardID = btnCardID.toString(16)
        if (btnCardID.length == 1) {
            btnCardID = "0" + btnCardID
        }
        btnCardID = "0x" + btnCardID
        this.OptionCardList(btnCardID)
        node.getChildByName("bg_black").active = this.sendCardList.indexOf(btnCardID) > -1
        this.btn_send.active = (this.sendCardList.length <= this.maxLength && this.sendCardList.length > 0)
    },

    SendGMCardList: function () {
        this.NetManager = app[app.subGameName + "_NetManager"]();
        let list = this.sendCardList
        let sendPack = {
            'handList': list,
        }
        app[app.subGameName + "_NetManager"]().SendPack("game.POKERSetWhiteHandList", sendPack)
    },

    OptionCardList: function (cardID) {
        let index = this.sendCardList.indexOf(cardID)
        if (index == -1) {
            this.sendCardList.push(cardID)
        } else {
            this.sendCardList.splice(index, 1)
        }
    },
});
